 //usage:
 //  zcc +embedded ns16450.o testconsole.c -otestconsole.bin -lim2 -lembedded_clib2
 
#include <stdlib.h>
#include <im2.h>
#include "ns16450.h"

#define INT_DISABLE()   asm("di")
#define INT_ENABLE()    asm("ei")

#define IOPORT1_BASE		0x0010
#define IOPORT0_BASE		0x0020
#define KEYBOARD_BASE		0x0030

#define CONSOLE_SIZE    (40*30)
volatile char* Console_Buffer =  0xF800;
volatile char* Color_Buffer   =  0xF000;


void main()
{
	int cursor_pos = 0;
	
	outp(IOPORT1_BASE, 0x01);
	outp(IOPORT0_BASE, 0x01);
	
     
  /* convert 24 MHz to 16*baudrate
         9600 : 0x9c
       115200 : 0x0d
  */
  init_uart(0x00, 0x0d); 

  outp(UART_BASE + THR, 'C');
  outp(UART_BASE + THR, 'o');
  outp(UART_BASE + THR, 'n');
  outp(UART_BASE + THR, 's');
 
  for (;;)
  {
    char UARTsr = 0;
    
    UARTsr = inp(UART_BASE + LSR);
    if (UARTsr & 0x01)
    {
      char ch;
      ch = inp(UART_BASE + RBR);
      outp(UART_BASE + THR, ch + 1);
      Console_Buffer[cursor_pos] = ch;
      Color_Buffer[cursor_pos]=0xF0;
      
      cursor_pos = (cursor_pos + 1) % CONSOLE_SIZE;
      
      if(ch == 4)
      {
      	int i;

      	outp(UART_BASE + THR, 'C');
      	outp(UART_BASE + THR, 'B');
      	outp(UART_BASE + THR, '\r');
      	outp(UART_BASE + THR, '\n');

      	for(i = 0; i < CONSOLE_SIZE; i++)
      	{
      		int delay;

      		for(delay = 0; delay < 100; delay++)
      	  {
      	  }
      	        		
      		outp(UART_BASE + THR,	Console_Buffer[i]);

      	}
      	
      }
    }
  }
}


